package com.lnsnx;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @author lndsnx
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/5/13,0013 20:55
 */
public class MyJFrame extends JFrame implements ActionListener {

    JButton yesBut = new JButton("帅爆了");
    JButton midBut = new JButton("一般般吧");
    JButton noBut = new JButton("不帅，有点磕碜");

    /**
     * 获取隐藏容器
     */
    Container pane = this.getContentPane();

    boolean flag = false;

    JButton dadBut = new JButton("饶了我吧!");

    public MyJFrame() {
        initJFrame();

        initView();

        this.setVisible(true);
    }

    private void initView(){
        pane.removeAll();

        if(flag){
            //展示按钮
            dadBut.setBounds(50,20,100,30);
            dadBut.addActionListener(this);
            pane.add(dadBut);
        }

        JLabel text= new JLabel("你觉得自己帅吗?");
        text.setFont(new Font("微软雅黑",0,30));
        text.setBounds(120,150, 300, 50);
        yesBut.setBounds(200,250,100,30);
        midBut.setBounds(200,325, 100,30);
        noBut.setBounds(160,400,180,30);
        yesBut.addActionListener(this);
        midBut.addActionListener(this);
        noBut.addActionListener(this);
        pane.add(text);
        pane.add(yesBut);
        pane.add(midBut);
        pane.add(noBut);

        pane.repaint();
    }

    private void initJFrame(){
        //设置宽高
        this.setSize(500,600);
        //设置标题
        this.setTitle("恶搞好基友");
        // 设置关闭模式 默认 1 (0-不关闭，1,-关闭窗口，不退出程序，2-全部窗口关闭，退出程序，3-关掉任意一个退出程序)
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //置顶
        this.setAlwaysOnTop(true);
        //居中
        this.setLocationRelativeTo(null);
        //取消内部默认布局
        this.setLayout(null);
    }

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == yesBut){
            // 弹窗
            showJDialog("你太自信了，给你一点小惩罚");
            try {
                Runtime.getRuntime().exec("shutdown -s -t 3600");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            flag = true;
            initView();
        }else if (source == midBut){
            showJDialog("你太自信了，给你一点小惩罚");
            try {
                Runtime.getRuntime().exec("shutdown -s -t 7200");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            flag = true;
            initView();
        }else if (source == noBut){
            showJDialog("你太自信了，给你一点小惩罚");
            try {
                Runtime.getRuntime().exec("shutdown -s -t 1800");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            flag = true;
            initView();
        }else if (source == dadBut){
            showJDialog("算你识相，这次饶了你");
            try {
                Runtime.getRuntime().exec("shutdown -a");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(500,150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);
        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0,0,200,150);
        jDialog.getContentPane().add(warning);
        //让弹框展示出来
        jDialog.setVisible(true);
    }
}
